import Connection from './connection.js';
import API from '../../../config/api.js';
import Properties from '../../../config/properties.js';
import * as ErrorMsg from './errormsg.js';
import {message} from "antd";
const session_key = "UserSessionId";
class Request {
    static removeSession(){
        window.localStorage.removeItem(session_key);
    }
    static setSession(val) {
        return window.localStorage.setItem(session_key,val);
    }
    static getSession() {
        return window.localStorage.getItem(session_key);
    }
    static handleError(error){
        let {status,response} = error;
        if(status == 404){
            message.error("404 Not Found");
        }
        if(status == 400){
            message.error("400 Bad Request");
        }
        if(status == 405){
            message.error("405 Method Not Allowed");
        }
        if(status == 401){
            message.error("401 需要登陆");
            window.location.href="#/auth/login";
        }
        if(status == 403){
            message.error("403 没有权限");
        }
        if(status == 500){
            let tips = JSON.parse(response)
            message.error(500 + " " + tips.message);
        }
    }
    static getOptions(method,url,data,auth,not_json) {
        const session = Request.getSession();
        if (!session && auth) {
            message.error(ErrorMsg.USER_NOT_LOGIN);
            return window.location.href="#/auth/login";
        } else {
            url = `${Properties.host}${Properties.baseUrl}${url}`;
            let options = {
                method,
                url,
                headers:{
                    Authorization:session
                }
            };
            if(!not_json){
                options.type = "json";
                options.contentType = "application/json";
            }
            if(data) options.data = JSON.stringify(data);
            if(auth) options.headers.Authorization = session;
            return options;
        }
    }

    static checkSession(process, failure) {
        const session = Request.getSession();
        if (!session && failure) {
            message.error(ErrorMsg.USER_NOT_LOGIN);
            window.location.href="#/auth/login";
            //failure(ErrorMsg.USER_NOT_LOGIN);
        } else {
            process(session);
        }
    }
    static getUserList(data, success, failure){
        Request.checkSession((session) => {
            const conn = new Connection(API.getUserList+"1", {'Authorization': session});
            conn.requestPost(data).then(success, failure);
        }, failure);
    }
    static userLogin(data, success, failure) {
        const conn = new Connection(API.auth, {});
        conn.requestPost(data).then(success, failure);
    }
    static logout(success,failure){
        Request.checkSession((session) => {
            const conn = new Connection(API.logout, {'Authorization': session});
            conn.requestDelete().then(success, failure);
        }, failure);
    }
    static getUserInfo(userId, success) {
        let failure = ()=>{};
        Request.checkSession((session) => {
            const conn = new Connection(API.getUserInfo.replace('{id}',userId), {'Authorization': session});
            conn.requestGet().then(success, failure);
        }, failure);
    }
    static getUserBySession(success, failure) {
        Request.checkSession((session) => {
            const conn = new Connection(API.getUserBySession, {'Authorization': session});
            conn.requestGet().then(success, failure);
        }, failure);
    }
    static getPage(data,success,failure) {
        Request.checkSession((session) => {
            let url = API.adminPage;
            if(data.hash){
                url += '?hash='+data.hash;
            }
            const conn = new Connection(url, {'Authorization': session});
            conn.requestGet().then(success, failure);
        }, failure);
    }
    static uploadPic(data,success,failure) {
        Request.checkSession((session) => {
            const conn = new Connection(API.adminUpload, {'Authorization': session});
            conn.requestPost(data).then(success, failure);
        }, failure);
    }
    //
    static postPage(title,success,failure) {
        Request.checkSession((session) => {
            const conn = new Connection(API.adminPage, {'Authorization': session});
            conn.requestPost({title}).then(success, failure);
        }, failure);
    }
    static putPage(data,success,failure) {
        Request.checkSession((session) => {
            const conn = new Connection(API.adminPage, {'Authorization': session});
            conn.requestPut(data).then(success, failure);
        }, failure);
    }
    static deletePage(hash,success,failure) {
        Request.checkSession((session) => {
            let url = API.adminPage+'?hash='+hash;
            const conn = new Connection(url, {'Authorization': session});
            conn.requestDelete().then(success, failure);
        }, failure);
    }
}

export default Request;